import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_kit_dsp/flutter_kit_dsp.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import '../../tool/assets.dart';

class InBottomGrid extends StatefulWidget {
  final String title;
  const InBottomGrid({super.key, required this.title});

  @override
  State<InBottomGrid> createState() => _InBottomGridState();
}

class _InBottomGridState extends State<InBottomGrid> {
  List<ChannelModel> list = [];
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    for (var ch in list) {
      ch.isSumSelect = false;
    }
    super.dispose();
  }

  Color getGridBgColor(text) {
    switch (widget.title) {
      case 'IN':
        return Color_theme;
      case 'OUT':
        return const Color(0xFF81B76C);
      case 'RIGHT':
        return '$text'.isEmpty ? Color_purple : Color_red;
      case 'LEFT':
        return '$text'.isEmpty ? Color_purple : Colors.white;
      default:
        return Colors.white;
    }
  }

  List<Widget> listWidget(width, height) {
    return [
      SizedBox(
          width: 80,
          child: Center(
              child: FittedBox(
                  fit: BoxFit.scaleDown, child: Text(widget.title, style: font40, textAlign: TextAlign.center)))),
      SizedBox(
        width: width,
        height: height,
        child: Obx(() {
          var model = globalController.rxModel.value;
          bool isIN = widget.title == 'IN';
          list = isIN ? model.inCh_list : model.ch_list;
          double gap = 5;
          int length = globalController.sourceType.value != SourceType.rca && isIN ? 2 : list.length;
          double w = min(height, (width - length * gap + gap) / length);
          gap = (width - w * length) / (length - 1);
          return Container(
              child: Row(
            spacing: gap,
            children: List.generate(length, (index) {
              ChannelModel ch = list[index];
              bool iscustom = globalController.sum.value.value == SumType.sumCUSTOM.value;
              var in_ch = isIN && iscustom ? ch : null;
              var out_ch = !isIN && iscustom ? ch : null;

              var text = '';
              if (globalController.sourceType.value == SourceType.rca || widget.title == 'OUT') {
                text = '${ch.num}';
              } else if (globalController.sourceType.value != SourceType.rca && isIN) {
                text = index % 2 == 0 ? "L" : "R";
              } else if (globalController.sourceType.value != SourceType.rca && widget.title == 'RIGHT') {
                text = index % 2 == 0 ? "" : "R";
              } else if (globalController.sourceType.value != SourceType.rca && widget.title == 'LEFT') {
                text = index % 2 == 0 ? "L" : "";
              }
              var bgcolor = getGridBgColor(text);
              return myTextButton(
                  width: w,
                  height: w,
                  radius: 4,
                  select: ch.isSumSelect,
                  color: (!iscustom && ch.isSumTarget) || (iscustom && ch.isSumSelect) ? bgcolor : Colors.transparent,
                  colors: ch.isSumSelect
                      ? [bgcolor, Colors.white]
                      : ch.isSumTarget
                          ? [bgcolor, bgcolor]
                          : [],
                  onTap: () => !iscustom
                      ? null
                      : globalController.updateSum(type: globalController.sum.value, in_ch: in_ch, out_ch: out_ch),
                  child:
                      Center(child: FittedBox(fit: BoxFit.scaleDown, child: Text(text, style: font40, maxLines: 1))));
            }).toList(),
          ));
        }),
      )
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: boxDecoration(5),
      padding: const EdgeInsets.all(5),
      child: LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
        ///动态计算item的宽度，gap
        double w_grid = constraints.maxWidth - 80 - 10;
        double h_gird = constraints.maxHeight;
        return Row(
          spacing: 5,
          textDirection: ['IN', 'OUT'].contains(widget.title) ? TextDirection.ltr : TextDirection.rtl, // 从右到左排列
          children: listWidget(w_grid, h_gird),
        );
      }),
    );
  }
}
